﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Model;
using EzDesk.Core.CoreImpl.Repositories.Interface;

namespace EzDesk.Core.CoreImpl.Repositories
{
    public class FeedRepository : IFeedRepository, IRepository<Feed>
    {
        private EzDeskDataContext _db;             

        public FeedRepository(EzDeskDataContext db)
        {
            _db = db;
        }

        public FeedRepository()
            : this(new EzDeskDataContext())
        { }

        #region IRepository<UpdateFeed> Members

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }

        public Guid Add(Feed model, bool saveChanges = true)
        {
            var entity = model.ToEntity();
            _db.ez_Feeds.InsertOnSubmit(entity);
            if (saveChanges)
            {
                SaveChanges();
            }
            return entity.FeedId;
        }

        public Feed GetById(Guid id)
        {
            Feed model = null;
            if (id != null)
            {
                model = _db.ez_Feeds.Where(p => p.FeedId == id)
                    .SingleOrDefault()
                    .ToModel();
            }
            return model;
        }

        public IQueryable<Feed> GetAll()
        {
            return from feed in _db.ez_Feeds
                   select new Feed()
                   {
                       Id = feed.FeedId,
                       FeedData = feed.FeedData,
                       FeedType = feed.FeedType,
                       ExpireOn = feed.ExpireOn,
                       CreateOn = feed.CreateOn,
                       TemplateId = feed.TemplateId,
                       UpdateOn = feed.UpdateOn,
                       OwnUserId = feed.OwnerId,
                   };
        }

        public void Delete(Guid id, bool saveChanges = true)
        {
            var entity = _db.ez_Feeds.Where(p => p.FeedId == id)
                .SingleOrDefault();
            _db.ez_Feeds.DeleteOnSubmit(entity);
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(Feed model, bool saveChanges = true)
        {
            var entity = _db.ez_Feeds.FirstOrDefault(p => p.FeedId == model.Id);
            if (entity != null)
            {
                ModelConvert.CopyValueFeed(entity, model);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }
        #endregion

        public void AddUserToFeed(Guid userId, Guid feedId, string feedDataEx, bool saveChanges = false)
        {
            _db.ez_Recipient_Feeds.InsertOnSubmit(
                new ez_Recipient_Feed()
                {
                    RecipientId = userId,
                    FeedId = feedId,
                    FeedDataEx= feedDataEx,
                    IsRead = false,
                    IsAvailable = true,
                    LastChangeOn = DateTime.Now,
                }
                );

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public bool UpdatePersonalFeedData(Guid userId, Guid feedId, string feedDataEx, bool saveChanges = false)
        {
            bool flag = true;
            var item = _db.ez_Recipient_Feeds
                .Where(p => p.RecipientId == userId && p.FeedId == feedId)
                .SingleOrDefault();
            if (item != null)
            {
                item.FeedDataEx = feedDataEx;
            }
            else
            {
                flag = false;
            }
            if (saveChanges)
            {
                SaveChanges();
            }
            return flag;
        }


        public IQueryable<Feed> GetByUser(Guid userId, bool? isRead, bool? isAvailable)
        {
            var query =
                from feed in _db.ez_Feeds
                join recipient_feed in _db.ez_Recipient_Feeds
                on feed.FeedId equals recipient_feed.FeedId
                where recipient_feed.RecipientId == userId
                select new Feed()
                {
                    Id = feed.FeedId,
                    FeedData = feed.FeedData,
                    FeedType = feed.FeedType,
                    ExpireOn = feed.ExpireOn,
                    CreateOn = feed.CreateOn,
                    TemplateId = feed.TemplateId,
                    UpdateOn = feed.UpdateOn,
                    OwnUserId = feed.OwnerId,
                    LastChangeOn = recipient_feed.LastChangeOn,
                    IsRead = recipient_feed.IsRead,
                    IsAvailable = recipient_feed.IsAvailable,
                };
            if (isRead.HasValue)
            {
                query = query.Where(p => p.IsRead == isRead.Value);
            }
            if (isAvailable.HasValue)
            {
                query = query.Where(p => p.IsAvailable == isAvailable.Value);
            }
            return query;
        }

        public void SetIsAvailable(Guid feedId, Guid recipientId, bool isAvailable, bool saveChanges = true)
        {
            var item = _db.ez_Recipient_Feeds
                .Where(p => p.FeedId == feedId && p.RecipientId == recipientId)
                .SingleOrDefault();
            item.IsAvailable = isAvailable;
            item.LastChangeOn = DateTime.Now;
            
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SetIsRead(Guid feedId, Guid recipientId, bool isRead, bool saveChanges = true)
        {
            var item = _db.ez_Recipient_Feeds
                .Where(p => p.FeedId == feedId && p.RecipientId == recipientId)
                .SingleOrDefault();
            item.IsRead = isRead;
            item.LastChangeOn = DateTime.Now;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public string GetPersonalFeedData(Guid userId, Guid feedId)
        {
            return _db.ez_Recipient_Feeds
                .Where(p => p.RecipientId == userId && p.FeedId == feedId)
                .Select(p => p.FeedDataEx)
                .SingleOrDefault();
        }
    }
}
